/*
 * Copyright (C) 2008-2010 Trinity <http://www.trinitycore.org/>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

/*#include "OutdoorPvPEI.h"
#include "OutdoorPvP.h"
#include "OutdoorPvPMgr.h"
#include "Player.h"
#include "WorldPacket.h"
#include "World.h"
#include "ObjectMgr.h"
#include "Language.h"

/*const uint32 EI_LANG_LOOSE_A[EI_TOWER_NUM] = {LANG_OPVP_EI_LOOSE_ORB_A};

const uint32 EI_LANG_LOOSE_H[EI_TOWER_NUM] = {LANG_OPVP_EI_LOOSE_ORB_H};

const uint32 EI_LANG_CAPTURE_A[EI_TOWER_NUM] = {LANG_OPVP_EI_CAPTURE_ORB_A};

const uint32 EI_LANG_CAPTURE_H[EI_TOWER_NUM] = {LANG_OPVP_EI_CAPTURE_ORB_H};

OutdoorPvPEI::OutdoorPvPEI()
{
    m_ControlOrb = 0;
    neutral_guid = 0;
    hguid = 0;
    aguid = 0;
    attack_timer = 60000 * 60; // 1 hour
}

bool OutdoorPvPEI::SetupOutdoorPvP()
{
    // add the zones affected by the pvp buff
    for (int i = 0; i < OutdoorPvPEIBuffZonesNum; ++i)
        RegisterZone(OutdoorPvPEIBuffZones[i]);

    neutral_guid = objmgr.AddGOData(EIOrb[EI_NEUTRAL].entry,
                                   EIOrb[EI_NEUTRAL].map,
                                   EIOrb[EI_NEUTRAL].x,
                                   EIOrb[EI_NEUTRAL].y,
                                   EIOrb[EI_NEUTRAL].z,
                                   EIOrb[EI_NEUTRAL].o);
    objmgr.AddGameobjectToGrid(neutral_guid, objmgr.GetGOData(neutral_guid));

    aguid = objmgr.AddGOData(EIOrb[EI_ALLIANCE].entry,
                                   EIOrb[EI_ALLIANCE].map,
                                   EIOrb[EI_ALLIANCE].x,
                                   EIOrb[EI_ALLIANCE].y,
                                   EIOrb[EI_ALLIANCE].z,
                                   EIOrb[EI_ALLIANCE].o);

    hguid = objmgr.AddGOData(EIOrb[EI_HORDE].entry,
                                   EIOrb[EI_HORDE].map,
                                   EIOrb[EI_HORDE].x,
                                   EIOrb[EI_HORDE].y,
                                   EIOrb[EI_HORDE].z,
                                   EIOrb[EI_HORDE].o);
    return true;
}

void OutdoorPvPEI::HandlePlayerEnterZone(Player * plr, uint32 zone)
{
    // add buffs
    if(plr->GetTeam() == ALLIANCE)
    {
        if(m_ControlOrb == 1)
            plr->CastSpell(plr,AllianceTeamBuff,true);
    }
    else
    {
        if(m_ControlOrb == 2)
            plr->CastSpell(plr,HordeTeamBuff,true);
    }
    OutdoorPvP::HandlePlayerEnterZone(plr,zone);
}

void OutdoorPvPEI::HandlePlayerLeaveZone(Player * plr, uint32 zone)
{
    // remove buffs
    if(plr->GetTeam() == ALLIANCE)
    {
        plr->RemoveAurasDueToSpell(AllianceTeamBuff);
    }
    else
    {
        plr->RemoveAurasDueToSpell(HordeTeamBuff);
    }
    OutdoorPvP::HandlePlayerLeaveZone(plr, zone);
}

bool OutdoorPvPEI::HandleOpenGo(Player *plr, uint64 guid)
{
    if(guid != neutral_guid)
        return true;

    if(plr->GetTeam() == ALLIANCE) /* Alliance 
    {
        objmgr.RemoveGameobjectFromGrid(guid, objmgr.GetGOData(guid));
        objmgr.AddGameobjectToGrid(aguid, objmgr.GetGOData(aguid));
    }
    else /* Horde 
    {
        objmgr.RemoveGameobjectFromGrid(guid, objmgr.GetGOData(guid));
        objmgr.AddGameobjectToGrid(hguid, objmgr.GetGOData(hguid));
    }

    return true;
}

bool OutdoorPvPEI::Update(uint32 diff)
{
    bool changed = false;
    if(true)//changed = OutdoorPvP::Update(diff))
    {
        if(attack_timer <= diff)
        {
            // Despawn Current Controlled Orb
            if(m_ControlOrb == 1)
            {
                sLog.outError("ALLIANCE DESPAWNED");
                objmgr.RemoveGameobjectFromGrid(aguid, objmgr.GetGOData(aguid));
                objmgr.AddGameobjectToGrid(neutral_guid, objmgr.GetGOData(neutral_guid));
            }
            else if(m_ControlOrb == 2)
            {
                sLog.outError("HORDE DESPAWNED");
                objmgr.RemoveGameobjectFromGrid(hguid, objmgr.GetGOData(hguid));
                objmgr.AddGameobjectToGrid(neutral_guid, objmgr.GetGOData(neutral_guid));
            }
            else // Skip timer reset if no one controls the orb
                return true;

            m_ControlOrb = 0; // Set orb control to neutral
            attack_timer = 60000 * 60; // 1 hour
        }
        else
            attack_timer -= diff;

        if(m_ControlOrb == 1)
            TeamApplyBuff(TEAM_ALLIANCE, AllianceTeamBuff, HordeTeamBuff);
        else if(m_ControlOrb == 2)
            TeamApplyBuff(TEAM_HORDE, HordeTeamBuff, AllianceTeamBuff);
        else
        {
            TeamCastSpell(TEAM_ALLIANCE, -AllianceTeamBuff);
            TeamCastSpell(TEAM_HORDE, -HordeTeamBuff);
        }
    }
    return changed;
}
/*

void OutdoorPvPEI::HandleKillImpl(Player *plr, Unit * killed)
{
    if(killed->GetTypeId() != TYPEID_PLAYER)
        return;
}*/
